import axios from 'axios'
import { ElMessage } from 'element-plus'

const defaultError = () => ElMessage.error('发生了一些错误，请联系管理员')
const defaultFailure = (message) => ElMessage.warning(message)
function post(url, data, success, failure = defaultFailure, error = defaultError) {
  axios
    .post(url, data, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      //   开启携带Cookie
      withCredentials: true
    })
    .then(({ data }) => {
      if (data.code === 200) success(data.message)
      else failure(data.message)
    })
    .catch(error)
}

function get(url, success, failure = defaultFailure, error = defaultError) {
  axios
    .get(url, {
      //   开启携带Cookie
      withCredentials: true
    })
    .then(({ data }) => {
      if (data.code === 200) success(data.message)
      else failure(data.message)
    })
    .catch(error)
}

export { get, post }
